package org.saxion.saktiehuis.service;

import java.util.List;
import org.saxion.saktiehuis.dao.GenericDao;
import org.saxion.saktiehuis.model.Order;
import org.saxion.saktiehuis.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Transactional
public class OrderService {
	@Autowired
	private GenericDao<Order, Long> orderDao;
	@Autowired
	private UserService userService;

	public OrderService() {
		super();
	}

	public List<Order> getOrdersFromUser(User user) {
		return orderDao.findAllByProperty("user_id", user.getId());
	}

	public Object getOrderById(Long id) {
		return orderDao.findById(id);
	}

	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public Order save(String username) {
		User user = userService.getUserByName(username);
		if (user != null) {
			Order order = new Order(user);
			orderDao.save(order);
			return order;
		}
		return null;
	}
}
